package code._15network.tcp.v4;

import java.io.*;
import java.net.Socket;

//发送文件
public class client4 {
    public static void main(String[] args) throws IOException {
        //创建socket
        Socket socket = new Socket("127.0.0.1", 9999);
        //创建文件输入流、读取源文件
        InputStreamReader fileIN = new InputStreamReader(new FileInputStream("fileTest\\a.txt"));
        //获取socket输出流OutputStream
        OutputStream socketOut = socket.getOutputStream();
        //用字符流发送、则用BufferOutputStreamWriter包装
        OutputStreamWriter out = new OutputStreamWriter(socketOut);
        //边读边发
        int readCount=0;
        char[] chars = new char[1024];
        boolean flag=true;
        while(flag){
            while((readCount=fileIN.read(chars))!=-1){
                out.write(chars,0,readCount);
                out.flush();
            }
            flag=false;
        }
        //发送完毕、关闭发送端、让对方知道发送完毕shutdownOutput()
        socket.shutdownOutput();
        //获取保存信息
        //获取socket输入流InputStream
        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        int read = inputStreamReader.read(chars);
        System.out.println(new String(chars,0,read));
        //释放资源
        socket.close();
        inputStreamReader.close();

    }
}
